﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Settings
{
    /// <summary>
    /// This object represents the methods of a Adv.
    /// </summary>
    internal class PaymentMethodDAO
    {

        #region general

        private static PaymentMethod LoadObj(IDataReader datareader)
        {
            PaymentMethod obj = new PaymentMethod();
            obj.PaymentMethodID = MpDataHelp.GetInt(datareader, "PaymentMethodID");
            obj.Name = MpDataHelp.GetString(datareader, "Name");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.DisplayOrder = MpDataHelp.GetInt(datareader, "DisplayOrder");
            return obj;
        }

        #endregion

        public static int PaymentMethod_Add(PaymentMethod obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_PaymentMethod_Add");
            db.AddOutParameter(dbCommand, "PaymentMethodID", DbType.Int32, obj.PaymentMethodID);
            db.AddInParameter(dbCommand, "Name", DbType.String, obj.Name);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, obj.DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@PaymentMethodID"));
            return -1;
        }
        public static int PaymentMethod_Update(PaymentMethod obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_PaymentMethod_Update");
            db.AddInParameter(dbCommand, "PaymentMethodID", DbType.Int32, obj.PaymentMethodID);
            db.AddInParameter(dbCommand, "Name", DbType.String, obj.Name);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, obj.DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int PaymentMethod_Delete(int PaymentMethodID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_PaymentMethod_Delete");
            db.AddInParameter(dbCommand, "PaymentMethodID", DbType.Int32, PaymentMethodID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<PaymentMethod> PaymentMethod_Select(int PaymentMethodID, int DisplayOrder, bool IsActive)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_PaymentMethod_Select");
            db.AddInParameter(dbCommand, "PaymentMethodID", DbType.Int32, PaymentMethodID);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
            List<PaymentMethod> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<PaymentMethod>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
